package org.particlethink.controller;


import org.particlethink.response.BaseResponse;
import org.particlethink.response.FileResponse;
import org.particlethink.service.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private StorageService storageService;

    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);


    @PostMapping("/upload")
    public BaseResponse<FileResponse> upload(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            String url = storageService.upload(file.getOriginalFilename(), file.getInputStream());
            return BaseResponse.success(new FileResponse(file.getOriginalFilename(), url));
        } catch (Exception e) {
            LOGGER.debug("userTokenAndRoleResponse:{}", e.getMessage());
            throw e;
        }

    }

}
